home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / ODUtils / AltPoly.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-17  |  8.0 KB  |  287 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        AltPoly.h
  3.  
  4.     Contains:    OpenDoc polygon: optional C++ savvy classes
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>     5/24/96    jpa        1.1MRD: pragma internal
  13.     
  14.     Theory of Operation:
  15.     
  16.         This is an alternate definition of ODPolygon and ODContour. The data format is
  17.         identical, but the structs defined here have a lot of useful methods including
  18.         constructors, accessors and conversion operators.
  19.         
  20.         To use these instead of the regular structs defined in Polygon.h, just include
  21.         this header file _before_ Polygon.h. An easy way to do this is to include it
  22.         first.
  23.         
  24.         QuickDraw GX users take note:
  25.         ODContour is identical in data format to a gxPolygon.
  26.         ODPolygonData (the data stored inside an ODPolygon) is identical in data
  27.             format to a gxPolygons <sic>.
  28.         See <GXTypes.h>.
  29.     
  30.         ** The way things are done has changed since A6. SOM wants all variable-sized
  31.         structures to adhere to a common data forma, where a small struct points to
  32.         the data and stores its size. This is so DSOM can tell how to copy the data
  33.         across an address-space boundary. The ODPolygon structure has most of the same
  34.         methods as before, but you can now create them directly. However, to get the
  35.         actual polygon data you'll need to call the GetData method. **
  36.     
  37.     In Progress:
  38. */
  39.  
  40.  
  41. #ifndef _ALTPOLY_
  42. #define _ALTPOLY_
  43.  
  44. #ifdef SOM_Module_OpenDoc_Polygon_defined
  45.     #error "Must include AltPoly.h *before* Polygon.xh!"
  46. #else
  47.     /* Make sure Polygon.xh does NOT get included later! */
  48.     #define SOM_Module_OpenDoc_Polygon_defined 2
  49. #endif
  50.  
  51. #ifndef _ODTYPES_
  52. #include "ODTypes.h"
  53. #endif
  54.  
  55. #ifndef _EXCEPT_
  56. #include <Except.h>                    /* For Destructo, used by ODTempPolygon */
  57. #endif
  58.  
  59. #include <stddef.h>                    /* for size_t */
  60.  
  61. #if _PLATFORM_MACINTOSH_
  62.     #ifndef __QUICKDRAW__
  63.     #include <QuickDraw.h>                /* for Region and Polygon types */
  64.     #endif
  65.     #ifndef __FIXMATH__
  66.     #include <FixMath.h>                /* Must include before GX headers... */
  67.     #endif
  68.     #ifndef __GXTYPES__
  69.     #include <GXTypes.h>                /* for gxShape type */
  70.     #endif
  71. #endif
  72.  
  73.  
  74. #ifdef PRAGMA_INTERNAL_SUPPORTED
  75. #pragma internal on
  76. #endif
  77.  
  78.  
  79. //==============================================================================
  80. // Classes used in this interface
  81. //==============================================================================
  82.  
  83. struct ODRect;
  84. class ODStorageUnit;
  85. class ODTransform;
  86.  
  87. //==============================================================================
  88. // ODContour
  89. //==============================================================================
  90.  
  91. struct ODContour
  92. {
  93.     public:
  94.     
  95.     ODSLong    nVertices;
  96.     ODPoint    vertex[1];        // Array size is actually nVertices
  97.     
  98.     ODContour* NextContour( )                const    {return (ODContour*)&vertex[nVertices];}
  99.     ODBoolean    IsRectangular( )            const;
  100.     ODBoolean    AsRectangle( ODRect* )        const;
  101. #if _PLATFORM_MACINTOSH_
  102.     PolyHandle    AsQDPolygon( )                const;
  103.     ODBoolean    HasExactRegion( )            const;
  104. #endif
  105.     
  106.     ODBoolean    operator== ( const ODContour& )            const;
  107.     ODBoolean    operator!= ( const ODContour &c )        const    {return !(*this==c);}
  108. };
  109.  
  110. //==============================================================================
  111. // ODPolygonData
  112. //==============================================================================
  113.  
  114. struct ODPolygonData {
  115.     ODSLong    nContours;                        // Number of contours
  116.     ODContour    firstContour;                // Rest of contours follow after first
  117. };
  118.  
  119. //==============================================================================
  120. // ODPolygon
  121. //==============================================================================
  122.  
  123. class ODPolygon
  124. {
  125.     public:
  126.     
  127.                     ODPolygon( );
  128. #if ODDebug
  129.                    ~ODPolygon( );        // Delete myself, but not data
  130. #endif
  131.     
  132.     void            Delete( );            // Delete myself & my data
  133.     void            Clear( );            // Just deletes my data
  134.     
  135.     // ACCESSORS:
  136.     
  137.     ODBoolean        HasData( )                        const    {return _length!=0;}
  138.     ODULong            GetDataSize( )                    const    {return _length;}
  139.     ODPolygonData*    GetData( )                        const    {return _buf;}
  140.     
  141.     void            SetData( const ODPolygonData* );    // Does not copy the data!
  142.     
  143.     ODSLong            GetNContours( )                    const;
  144.     ODContour*        FirstContour( );
  145.     const ODContour*FirstContour( )                    const;
  146.     
  147.     // GEOMETRY:
  148.     
  149.     void        ComputeBoundingBox( ODRect* )        const;
  150.     ODBoolean    IsRectangular( )                    const;
  151.     void        Transform( Environment*, ODTransform* );
  152.     
  153.     ODBoolean    operator== ( ODPolygon& )            const;
  154.     ODBoolean    operator!= ( ODPolygon& p )            const    {return !(*this==p);}
  155.     
  156.     ODSLong    Contains( ODPoint )                        const;
  157.     ODBoolean    IsEmpty( )                            const;
  158.     
  159.     // CONVERSIONS:
  160.     
  161.     ODBoolean    AsRectangle( ODRect* )                const;    // False if nonrectangular
  162. #if _PLATFORM_MACINTOSH_
  163.     ODBoolean    HasExactRegion( )                    const;
  164.     RgnHandle    AsQDRegion( )                        const;
  165.     gxShape        AsGXShape( )                        const;
  166. #endif
  167.     
  168.     // ALLOCATION:
  169.     
  170.     ODPolygon*    SetNVertices( ODSLong nVertices );
  171.     ODPolygon*    SetVertices( ODSLong nVertices, const ODPoint *vertices );
  172.     ODPolygon*    SetContours( ODSLong nContours, const ODSLong *contourVertices );
  173.     ODPolygon*    SetRect( const ODRect& );
  174.  
  175.     ODPolygon*    Copy( )                                const;
  176.     ODPolygon*    CopyFrom( const ODPolygon& );
  177. #if _PLATFORM_MACINTOSH_
  178.     ODPolygon*    CopyFrom( gxShape );        // Accepts rect, polygon(s), path(s)
  179. #endif
  180.     ODPolygon*    MoveFrom( ODPolygon& );        // Justs adjusts pointers, no copying
  181.     
  182.     // INPUT/OUTPUT:
  183.     
  184.     ODPolygon*    ReadFrom( Environment*, ODStorageUnit* );
  185.     ODPolygon*    WriteTo( Environment*, ODStorageUnit* )        const;
  186.     
  187.     private:
  188.     
  189.     void        Realloc( ODULong dataSize );
  190.     
  191.     // DATA MEMBERS:
  192.     
  193.     unsigned long _maximum;                        // Exact same data as an ODByteArray
  194.     unsigned long _length;
  195.     ODPolygonData *_buf;
  196. };
  197.  
  198.  
  199. //==============================================================================
  200. // ODTempPolygon
  201. //==============================================================================
  202.  
  203. /*    ODTempPolygon is a polygon whose destructor disposes of its data.
  204.     This is useful if you have a local variable that's a temporary polygon
  205.     and you want to make sure the data gets disposed.
  206.     This _is_ exception-safe: inheriting from Destructo guarantees that the
  207.     data will be cleaned up even if an exception is thrown.
  208. */
  209.  
  210. class ODTempPolygon :public ODPolygon, Destructo
  211. {
  212. public:
  213.     ODTempPolygon( );
  214.    ~ODTempPolygon( );
  215. };
  216.  
  217.  
  218. /*    ODTempPolygonPtr is a _pointer_ to a polygon, whose destructor deletes
  219.     the polygon structure (and its data.) Yes, it is a class, but due to the
  220.     magic of operator overloading it can be used just as a pointer. See the
  221.     implementation of ODPolygon::Copy in AltPoly.cpp for an example. */
  222.  
  223. class ODTempPolygonPtr :Destructo
  224. {
  225. public:
  226.     ODTempPolygonPtr( );
  227.     ODTempPolygonPtr( ODPolygon* );
  228.     ~ODTempPolygonPtr( );
  229.     operator ODPolygon* ( )                    {return fPoly;}
  230.     ODPolygon* operator-> ( )                {return fPoly;}
  231.     ODPolygon* operator= ( ODPolygon *p )    {return (fPoly=p);}
  232.     ODPolygon* DontDelete( )                {ODPolygon* temp=fPoly; fPoly=kODNULL; return temp;}
  233.     
  234. private:
  235.     ODPolygon *fPoly;
  236. };
  237.  
  238.  
  239. /*    TempGXShape is a temporary reference to a gxShape. It will be released when
  240.     the reference goes out of scope. For an example, see the implementation of
  241.     ODPolygon::CopyFrom( gxShape ). */
  242.  
  243. class TempGXShape :Destructo
  244. {
  245. public:
  246.     TempGXShape( );
  247.     TempGXShape( gxShape );
  248.     ~TempGXShape( );
  249.     operator gxShape ( )                {return fShape;}
  250.     gxShape operator= ( gxShape s )        {return (fShape=s);}
  251.     gxShape DontRelease( )                {gxShape temp=fShape; fShape=kODNULL; return temp;}
  252.     
  253. private:
  254.     gxShape fShape;
  255. };
  256.  
  257.  
  258. //==============================================================================
  259. // Polygon Edge Iterator
  260. //==============================================================================
  261.  
  262. class PolyEdgeIterator {
  263.     public:
  264.  
  265.     PolyEdgeIterator( const ODPolygon* );
  266.     
  267.     void        CurrentEdge( const ODPoint* &v1, const ODPoint* &v2 );
  268.     const ODContour* CurrentContour( )            {return fCurContour;}
  269.     long        CurrentContourIndex( )            {return fCurContourIndex;}
  270.     long        CurrentEdgeIndex( )                {return fCurVertex;}
  271.     
  272.     ODBoolean    Next( );
  273.     ODBoolean    IsNotComplete( );
  274.     
  275.     private:
  276.     const ODPolygon*    const fPoly;
  277.     const ODContour*          fCurContour;
  278.     long                      fCurContourIndex;
  279.     long                      fCurVertex;
  280. };
  281.  
  282.  
  283. #ifdef PRAGMA_INTERNAL_SUPPORTED
  284. #pragma internal off
  285. #endif
  286.  
  287. #endif //_ALTPOLY_